#include "Invader.h"
#include "Ship.h"
#include "VGCVirtualGameConsole.h"

using namespace std;

Invader::Invader(double x, double y, double direction, VGCTimer timeOfDeath) :
	CGameEntity(CImage("res/sprites/CInvader.png", 4, 8), x, y, 20.0, direction),
	rotationTimer(VGCClock::openTimer(3.0)),
	timeOfDeath(timeOfDeath) {}

Invader::~Invader() {
	VGCClock::closeTimer(rotationTimer);
	VGCClock::closeTimer(timeOfDeath);
}

int Invader::frameIndexY(const double &time) {
	while (getDirection() > (3.14159265 * 2.0)) {
		setDirection(getDirection() - (3.14159265 * 2.0));
	}

	return (int) (getDirection() / (3.14159265 / 4.0)) % 8;
}

void Invader::step(const double &time) {
	CGameEntity::step(time);

	if (VGCClock::isExpired(rotationTimer)) {
		setDirection((rand() % 8) * (3.14159265 / 4.0));
		rotationTimer = VGCClock::openTimer(3.0);
	}

	if (VGCClock::isExpired(timeOfDeath)) {
		setDisposed(true);
	}
}

bool Invader::die() {
	if (isDisposed()) {
		return false;
	} else {
		setDisposed(true);
		Ship::instance().setScore(Ship::instance().getScore() + 100);
		return true;
	}
}